<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>商家基本资料</span>
      </div>
    </template>
    <el-row>
      <el-col :span="12">
        <el-form
          :model="businessInfo"
          :rules="rules"
          label-width="100px"
          size="large"
        >
          <el-form-item label="用户名" prop="name">
            <el-input v-model="businessInfo.name"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="telephone">
            <el-input v-model="businessInfo.telephone"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="businessInfo.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="updateBusinessInfo"
              >提交修改</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>

<script setup>
import { ref } from "vue";
import { useBusinessInfoStore } from "@/store/businessInfo";
import { businessInfoUpdateService } from "@/api/Business"
import { ElMessage } from "element-plus";
const businessInfoStore = useBusinessInfoStore();
const businessInfo = ref({ ...businessInfoStore.businessInfo });
const rules = {
  name: [
    { required: true, message: "请输入用户昵称", trigger: "blur" },
    {
      pattern: /^\S{2,10}$/,
      message: "用户名必须是2-10位的非空字符串",
      trigger: "blur",
    },
  ],
  telephone: [
    { required: true, message: "请输入电话号码", trigger: "blur" },
    {
      pattern: /^\d{11}$/,
      message: "用户电话号码必须是11位的数字",
      trigger: "blur",
    },
  ],
  email: [
    { required: true, message: "请输入用户邮箱", trigger: "blur" },
    { type: "email", message: "邮箱格式不正确", trigger: "blur" },
  ],
};

//修改个人信息函数
const updateBusinessInfo = async () => {
  const result = await businessInfoUpdateService(businessInfo.value);
  ElMessage.success(result.message ? result.message : "修改成功");
  if (result.message) {
    businessInfoStore.setBusinessInfo(businessInfo.value);
  }
};
</script>